using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Life
{
    /// <summary>
    /// TextControl is a control that displays a single string of text. By default, the
    /// size is computed from the given text and spritefont.
    /// </summary>
    public class TextControl : Control
    {
        protected SpriteFont _font;
        protected string _text;

        public Color Color;

        // Text to draw
        public virtual string Text
        {
            get { return _text; }
            set
            {
                if (_text != value)
                {
                    _text = value;
                    InvalidateAutoSize();
                }
            }
        }

        // Font to use
        public virtual SpriteFont Font
        {
            get { return Font; }
            set
            {
                if (_font != value)
                {
                    _font = value;
                    InvalidateAutoSize();
                }
            }
        }

        public TextControl(string text, SpriteFont font)
            : this(text, font, Color.White, Vector2.Zero)
        {
        }

        public TextControl(string text, SpriteFont font, Color color)
            : this(text, font, color, Vector2.Zero)
        {
        }

        public TextControl(string text, SpriteFont font, Color color, Vector2 position)
        {
            _text = text;
            _font = font;
            this.Position = position;
            this.Color = color;
        }

        public TextControl(string text, SpriteFont font, Color color, Vector2 position, Vector2 size)
            : this(text, font, color, position)
        {
            this.Size = size;
        }

        public override void Draw(DrawContext context)
        {
            // TODO: Should the measuring and positioning be done elsewhere, like an Update method?

            base.Draw(context);

            // Draw the text centered in the control

            // Compute the button's rectangle
            Vector2 absPos = Position + context.DrawOffset;
            Rectangle r = new Rectangle(
                (int)absPos.X,
                (int)absPos.Y,
                (int)Size.X,
                (int)Size.Y);

            Vector2 textSize = Vector2.Zero;
            try 
            {
                if (Text != null) 
                    textSize = _font.MeasureString(Text);
            }
            catch (ArgumentException)
            {
                // TODO: If we hit this, the _font DefaultCharacter property is not available.
            }

            Vector2 textPosition = new Vector2(r.Center.X, r.Center.Y) - textSize / 2f;
            textPosition.X = (int)textPosition.X;
            textPosition.Y = (int)textPosition.Y;

            context.SpriteBatch.DrawString(_font, Text, textPosition, Color);
        }

        public override Vector2 ComputeSize()
        {
            return Text == null ? Vector2.Zero : _font.MeasureString(Text);
        }
    }
}
